import { doLoginApi } from '@/api/loginApi'
import { setToken } from '@/utils/token'
import { setName } from '@/utils/name'
import sessionstore from '@/utils/sessionStore'
export default{
    state:{
        token:'',
        uname:'',
        uid:0,
        role:'',
        menu:[]
    },
    mutations:{
        addToken(state,token){
            state.token = token
            setToken(token)
        },
        setUname(state,uname){
            state.uname = uname
            setName(uname)
        },
        setUid(state,uid){
            state.uid = uid
        },
        setRole(state,role){
            state.role = role
            sessionstore.set('role',role)
        },
        setMenu(state,menu){
            state.menu = menu
            sessionstore.set('menu',menu)
        },
    },
    actions:{
        async loginAction({ commit },userData){
            let ret = await doLoginApi(userData)
            if(ret.code === 0){
                commit('addToken',ret.data.token)
                commit('setUname',ret.data.name)
                commit('setUid',ret.data.uid)
                commit('setRole',ret.role)
                commit('setMenu',ret.menu)
            }
            return  ret.code === 0 ? true : false
        }
    },
    /* getters:{
        token:state => state.token || getToken()
    } */
}